/* R. J. Cano, rev. Aug 19 2014

=============================================
Additional information for abs(A242072)
=============================================

The content of this file demonstrates the
concatenation properties mentioned at
the comments for this sequence.

------------------------------------------------

Remarks

Dear reader:

1) Please notice that here is strongly focused
   the attention in the absolute values for
   these terms.

2) You also can load this as a GP-PARI script.

-----------------------------------------------

Observations.

Nested/concatenated terms. Patterns dictionary:

1) The patterns are named in lexicographic order
   of appearance.

2) The replacements should be made according the
   length of the patterns, shortest patterns first.
   
3) Define more patterns if necessary.

Given x=10 a positive integer (Specifically 10 for OEIS).

By replacement priority, the patterns are listed below:

D=x=10
E=(x-1)*x=90

A=D/D=1
B=(E-D)/D=8
C=E/D=9

{1}=DE
{2}=BCD
{3}=C{1}
{4}=E{2}
{5}=E{3}
{6}=E{4}
{7}={1}{4}
{8}=A{7}
{9}=BBE{5}
{10}=C{8}
{11}=E{9}
{12}=E{10}
{13}={1}{10}
{14}=A{13}
{15}=BBE{11}
{16}=C{14}
{17}=E{15}
{18}=E{16}
{19}={1}{16}
{20}=A{19}
{21}=BBE{17}
{22}=C{20}
{23}=D{22}
{24}=A{23}
{25}=A{24}
{26}=A{25}
{27}=BBBBE{21}
{28}=C{26}
{29}=E{27}
{30}=E{28}
{31}={1}{28}
{32}=A{31}
{33}=A{32}
{34}=A{33}
{35}=BBBBE{29}
{36}=C{34}
{37}=D{36}
{38}=A{37}
{39}=BBE{35}
{40}=C{38}
{41}=E{39}
{42}=E{40}
{43}={1}{40}
{44}=A{43}
{45}=BBE{41}
{46}=C{44}
{47}=D{46}
{48}=A{47}
{49}=A{48}
{50}=A{49}
{51}=BBBBE{45}
{52}=C{50}
{53}=A{52}
{54}=A{53}
{55}=A{54}
{56}=A{55}
{57}=BBBBE{51}
{58}=C{56}
{59}=E{57}
{60}=E{58}
{61}={1}{58}
{62}=A{61}
{63}=A{62}
{64}=A{63}
{65}=BBBBE{59}
{66}=C{64}
{67}=D{66}
{68}=A{67}
{69}=BBE{65}
{70}=C{68}
{71}=E{69}
{72}=E{70}
{73}={1}{70}
{74}=A{73}
{75}=A{74}
{76}=A{75}
{77}=BBBBE{71}
{78}=C{76}
{79}=D{78}
{80}=A{79}
{81}=BBE{77}
{82}=C{80}
{83}=D{82}
{84}=A{83}
{85}=A{84}
{86}=A{85}
{87}=BBBBE{81}
{88}=C{86}
{89}=D{88}
{90}=A{89}
{91}=A{90}
{92}=A{91}
{93}=A{92}
{94}=A{93}
{95}=BBBBBBE{87}
{96}=C{94}
{97}=D{96}
{98}=A{97}
{99}=BBE{95}
{100}=C{98}
{101}=E{99}
{102}=E{100}
{103}={1}{100}
{104}=A{103}
{105}=BBE{101}
{106}=C{104}
{107}=E{105}
{108}=E{106}
{109}={1}{106}
{110}=A{109}
{111}=BBE{107}
{112}=C{110}
{113}=D{112}
{114}=A{113}
{115}=A{114}
{116}=A{115}
{117}=A{116}
{118}=A{117}
{119}=A{118}
{120}=A{119}
{121}=A{120}
{122}=A{121}
{123}=A{122}
{124}=A{123}
{125}=BBBBBBBBBBBBE{111}
{126}=C{124}
{127}=D{126}
{128}=A{127}
{129}=BBE{125}
{130}=C{128}
{131}=D{130}
{132}=A{131}
{133}=A{132}
{134}=A{133}
{135}=BBBBE{129}
{136}=C{134}
{137}=E{135}
{138}=E{136}
{139}={1}{136}

...And so on.

For example, "{11}=E{9}" should be read as:

"Pattern #11 is (by definition) the concatenation of patterns E and #9".

The purpose is to encode any given sample composed with the first M terms of
this sequence in the form:

D, E, {1}, {2}, {3}, ...., {k}

This is, like a simple enumeration in increasing order of the defined patterns.

Question:
Which would be the semi-numerical algorithm underlying to this sequence,
based on this replacements scheme?

*/

ap(n,x)={my(s);forprime(p=1,n,s+=x^p);s}
a(n,x=10)={abs((x^(n+1)-1)/(x-1)-2*ap(n,x)-1)}

/* Extension: Aug 7 2014 */

av(n,x,y=x)=digits(a(n,x),y); /* Digits of a(n,x) "when it is expressed in base y" 
                               (Be careful!, perhaps it should be always y>=x) */
fixMe(i,B)=if(i>=B,B-1,i);

letter(i)=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"][fixMe(i,36)+1];

aStr(n,x=10,y=x)={my(s="",w=av(n,x,y));for(j=1,#w,s=Str(s,letter(w[j])));s}

/* Deletes leading character given by str1[1], str1[2], ... from  str2 (recursive version) */
leadingChrDel(str1,str2,index=1)={my(ans="",V0=Vecsmall("0"),V1=Vecsmall(str1),V2=Vecsmall(str2),j);while(V2[j++]==V1[index],);while(j<=#str2,ans=Str(ans,letter(V2[j]-V0[1]));j++);if(index==#str1,ans,leadingChrDel(str1,ans,index+1))};

demo(terms=2500)={

sample002=vector(terms,j,aStr(j,2));  \\ Binary
sample003=vector(terms,j,aStr(j,3));  \\ Base 3
sample004=vector(terms,j,aStr(j,4));  \\ Base 4
sample008=vector(terms,j,aStr(j,8));  \\ Octal 
sampleDef=vector(terms,j,aStr(j));    \\ By default: Decimal or Base10
sample012=vector(terms,j,aStr(j,12)); \\ Duodecimal
sample016=vector(terms,j,aStr(j,16)); \\ Hexadecimal

\\ From Base 4 onward, terms with the same offset must have identical lengths in digits/letters.
\\ sampleDef is taken as reference ("cmp" stands for "comparison"). 

\\ It is expected that this comparison fails for Binary: Since there

\\ are less basic elements (less than 4, just 2, "0" and "1").
\\ (For Base 3 apparently it should fail, but observe there that One of the basic elements
\\ actually is duplicated)

\\ This initial obvious comparison proves that the test works fine.

cmpDef=(#sampleDef==#sampleDef)*prod(j=1,#sampleDef,#sampleDef[j]==#sampleDef[j]);

if(cmpDef,print("\n\nThe proposed comparison makes sense and works."),print("\n\nHey!: Something is wrong here.");quit());

\\ The expected fail.

cmp002=(#sampleDef==#sample002)*prod(j=1,#sampleDef,#sampleDef[j]==#sample002[j]);

\\ The empirical* verification of the observed and PRESUMABLY GENERAL properties.
\\ (*Note: Not exhaustive at all)

cmp003=(#sampleDef==#sample003)*prod(j=1,#sampleDef,#sampleDef[j]==#sample003[j]);
cmp004=(#sampleDef==#sample004)*prod(j=1,#sampleDef,#sampleDef[j]==#sample004[j]);
cmp008=(#sampleDef==#sample008)*prod(j=1,#sampleDef,#sampleDef[j]==#sample008[j]);
cmp012=(#sampleDef==#sample012)*prod(j=1,#sampleDef,#sampleDef[j]==#sample012[j]);
cmp016=(#sampleDef==#sample016)*prod(j=1,#sampleDef,#sampleDef[j]==#sample016[j]);

/* By having in account the leading zeros problem with x=2 or binary */

sampleDefAltered=vector(terms,j,leadingChrDel("8",sampleDef[j]));
cmp002again=(#sampleDefAltered==#sample002)*prod(j=1,#sampleDefAltered,#sampleDefAltered[j]==#sample002[j]);

answer=(!cmp002)*cmp003*cmp004*cmp008*cmp012*cmp016*cmp002again;

print("\n\n\n\n...Done!.\n\nConclusion. Are we right relative to our initial & informal description for this seq.?: "if(answer,"Yes.","Nope!, Oops."));

}

/*
  Final comment/invitation from the author:
  
  <<Let me propose briefly the following experiment based on the comments on this sequence.
    It can be done without none line of code nor specialized hardware. Titled "Live LAB":
  
  Just pick a scientific calculator with capabilities for basic arithmetic in other popular
  positional systems like binary, octal, and hexadecimal (I have a CASIO FX-570ES that can
  do that). You cannot type there "abs(10^1-10^2-10^3+10^4-10^5+10^6-10^7)" in Hexadecimal,
  but you can type this other expression instead:
  
  "10-10*10-10*10*10+10*10*10*10-10*10*10*10*10+10*10*10*10*10*10-10*10*10*10*10*10*10"
  
  please ensure that the +/- sings and repeated "10*"s are correct and then hit "=";
  You'll get "F0F0EF10" in the answer and... it is wrong!, but wait, just press the 
  key for displaying such same number in Decimal and you'll find that there still 
  being pending to apply the absolute value. There is shown "-252645616" In decimal,
  so multiply what is displayed by -1, and go back to hexadecimal by hitting the proper
  key. Now it is shown "0F0F10F0", and by replacing E with 8 and F with 9 it will looks
  like "09091090", precisely abs(a(7)) at the data field or in the b-file... (i) Now repeat
  it in the other available bases excepting binary and base-3, (ii) Then try the same procedure with
  other terms>>.

  ...That's all for now. - EOF */